package com.fanfanwang.ffw.app.asynctask;

import java.util.LinkedHashMap;

import android.app.Activity;
import android.text.TextUtils;

import com.fanfanwang.ffw.app.bean.SucGetMsgListBean;
import com.fanfanwang.ffw.app.common.Md5Util;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.fragment.GlyMessageFragment;
import com.fanfanwang.ffw.app.net.HttpRequest.HttpRequestException;
import com.fanfanwang.ffw.app.net.IssAsyncTask;
import com.fanfanwang.ffw.app.net.IssNetLib;
import com.fanfanwang.ffw.app.net.WebServiceHelper;
import com.fanfanwang.ffw.app.service.PushNewMessageService;
import com.fanfanwang.ffw.app.tenant.R;

public class GetMsgListAsyncTask extends
		IssAsyncTask<String, String, SucGetMsgListBean> {
	private Activity mActivity;
	private String parking_id;
	private String page;
	private String pagesize;
	private String type;

	// getMsgList($apiusername,$apipassword,$parking_id,$page,$pagesize)
	public GetMsgListAsyncTask(Activity activity, String parking_id,
			String page, String pagesize,String type) {
		super(activity);
		mActivity = activity;
		this.parking_id = parking_id;
		this.page = page;
		this.pagesize = pagesize;
		this.type=type;
	}

	@Override
	protected SucGetMsgListBean doInBackground(String... params) {
		// TODO addAuctionPrice($apiusername,$apipassword,$userid,$pid,$price)
		SucGetMsgListBean sucGetMsgListBean = null;
		LinkedHashMap<String, Object> paramsMap = new LinkedHashMap<String, Object>();
		paramsMap.put("apiusername", "parking");
		paramsMap.put("apipassword", Md5Util.getMD5To32("parking"));
		paramsMap.put("parking_id", parking_id);
		paramsMap.put("page", page);
		paramsMap.put("pagesize", pagesize);
		paramsMap.put("type", type);
		try {
			sucGetMsgListBean = IssNetLib.getInstance(mActivity).getMsgList(
					WebServiceHelper._GETMSGLIST, paramsMap);

			// TODO 自己填冲数据

		} catch (HttpRequestException e) {
			e.printStackTrace();
			// exception = getResources().getString(R.string.e_net_error);
		} catch (Exception e) {// JSONException
			e.printStackTrace();
			// exception = getResources().getString(R.string.e_json);
		}
		return sucGetMsgListBean;
	}

	@Override
	protected void onPostExecute(SucGetMsgListBean result) {
		super.onPostExecute(result);
		GlyMessageFragment.mPullToRefreshListView.onRefreshComplete();
		if (exception != null) {
			ToastAlone.showToast(mActivity, exception);
			return;
		}
		if (result != null) {
			if (result.status == 1) {
				if (result.data != null) {
					if("1".equals(type))//这里是为了区分，是不是新预约消息提醒使用的。
					{
					GlyMessageFragment.igetMessageList
							.setMessageList(result.data);
					}else if("0".equals(type)){
						PushNewMessageService.iGetIsNewMessage.setIsNewMessage(result.data);
					}
				}
			} else if (result.status == 0) {
				if (result.msg != null && !TextUtils.isEmpty(result.msg)) {
					ToastAlone.showToast(mActivity,
							com.fanfanwang.ffw.app.AppConstant
									.URLDecoderString(result.msg));
				} else {
					ToastAlone.showToast(mActivity, mActivity.getResources()
							.getString(R.string.e_net_error));
				}
			}
		}
	}
}
